package com.devicemantra.coordinate.tasks;

import java.util.ArrayList;

import com.devicemantra.coordinate.interfaces.queue.CQueueFactory;
import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskHandle;
import com.google.appengine.api.labs.taskqueue.TaskOptions;

import static com.devicemantra.coordinate.interfaces.queue.CQueueFactory.*;

public class CTaskFactory {
	
	public static boolean addTaskToQueue(CTask pTask)
	{
		boolean isSuccess = false;
		Queue lQueue = pTask.getQueue();
		String lTaskUrl = pTask.getTaskUrl();
		ArrayList lParamList = pTask.getParameters();
		
		if( lQueue == null )
		{
			System.out.println("Error : Such a Queue does not exist " + lQueue.getQueueName());
			return isSuccess;
		}
		
		/* First let us set the url for the task */
		TaskOptions lTaskOptions = TaskOptions.Builder.url(lTaskUrl);
		for(int lCounter = 0; lCounter < lParamList.size(); lCounter++)
		{
			CNameVal lNameVal = (CNameVal)lParamList.get(lCounter);
			lTaskOptions = lTaskOptions.param(lNameVal.getName(),lNameVal.getValue());
		}
		
		/* Finally add the task to the intended queue */
		TaskHandle lTaskHandle =null;
		try
		{
			lTaskHandle = lQueue.add(lTaskOptions);
		} catch(Exception e) {
			System.out.println("Exception caught : While adding task to " + lQueue.getQueueName() + " Description :" + e);
			return false;
		}
		
		/* Log an entry to the database about the insertion of the task */
		System.out.println("Task Name : " + lTaskHandle.getName() + " has been queued on " + lQueue.getQueueName()); 
		isSuccess = true;
		return isSuccess;
	}
	
	public static CTask createNewTask() { 
		/*TODO: Do some book keeping of how many tasks were created etc */
		CTask lTask = new CTask();
		return lTask;
	}
	
	public static CTask createNewEmailTask() { 
		/*TODO: Do some book keeping of how many tasks were created etc */
		CTask lTask = createNewTask();
		Queue lQueue = CQueueFactory.getQueue(EMAIL_QUEUE);
		if( lQueue != null) {
			lTask.setQueue(lQueue);
		}else {
			lTask = null;
			System.out.println("CException: Could not find Queue");
		}
		return lTask;
	}
	
	public static CTask createNewSMSTask() { 
		/*TODO: Do some book keeping of how many tasks were created etc */
		CTask lTask = createNewTask();
		Queue lQueue = CQueueFactory.getQueue(SMS_QUEUE);
		if( lQueue != null) {
			lTask.setQueue(lQueue);
		}else {
			lTask = null;
			System.out.println("CException: Could not find Queue");
		}
		return lTask;
	}
	
	public static CTask createNewCallTask() { 
		/*TODO: Do some book keeping of how many tasks were created etc */
		CTask lTask = createNewTask();
		Queue lQueue = CQueueFactory.getQueue(CALL_QUEUE);
		if( lQueue != null) {
			lTask.setQueue(lQueue);
		}else {
			lTask = null;
			System.out.println("CException: Could not find Queue");
		}
		return lTask;
	}
	
	
}

